package com.lele.file;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;



public class EntityFile extends SuperFile{
	public EntityFile(SuperFile sf){
		this.project_root = sf.getProject_root();
		this.file_url = sf.getFile_url()+"entity\\";
		this.package_url = sf.getPackage_url()+"\\entity";
		this.entityClass = sf.getEntityClass();
		this.DB_name = sf.getDB_name();
		this.fields = sf.getFields();
		this.entityName = sf.getEntityName();
		
	}
	public EntityFile(){
		
	}
	public String getFileURL() {
		StringBuilder s = new StringBuilder();
		s = s.append(file_url).append(entityClass).append(".java");
		return s.toString();
	}
	
	public String getContent(){
		StringBuilder s = new StringBuilder();
		String head_package = package_url.replace("\\", ".");
		String fh = ";";
		s = s.append("package ").append(head_package).append(fh);
		s.append("\r\n\r\n");
		s.append("import java.io.Serializable;");
		s.append("\r\n");
		s.append("import javax.persistence.Column;");
		s.append("\r\n");
		s.append("import javax.persistence.Entity;");
		s.append("\r\n");
		s.append("import javax.persistence.GeneratedValue;");
		s.append("\r\n");
		s.append("import javax.persistence.GenerationType;");
		s.append("\r\n");
		s.append("import javax.persistence.Id;");
		s.append("\r\n");
		s.append("import javax.persistence.Table;");
		s.append("\r\n\r\n");
		s.append("import org.springframework.context.annotation.Scope;");
		s.append("\r\n\r\n");
		s.append("@Entity");
		s.append("\r\n");
		s.append("@Scope(\"prototype\")");
		s.append("\r\n");
		s.append("@Table(name=\"").append(entityClass.toLowerCase()).append("\",catalog=\"").append(DB_name).append("\")");
		s.append("\r\n");
		s.append("public class ").append(entityClass).append(" implements Serializable {");
		s.append("\r\n\r\n");
		s.append("    private static final long serialVersionUID = 1L;");
		s.append("\r\n\r\n");
		if (fields.size()>0) {
			Set<Entry<String, String>> maps = fields.entrySet();
	        Iterator<Entry<String, String>> temps = maps.iterator();
	        while (temps.hasNext())
	        {
	            Entry<String, String> keys1 = (Entry<String, String>)temps.next();
	            String value = keys1.getKey();
	           
	            String Key = fields.get(value);
	            //System.out.println(Key+":"+value);
	            if (Key.equals("Char")) {
	            	Key = "String";
				}
	           
	            if (Key.indexOf("|PK")>0) {
					Key = Key.substring(0,Key.indexOf("|PK"));
				}
	            s.append("    private ").append(Key).append(" ").append(value).append(fh);
	            s.append("\r\n");
	            //System.err.println("getMap001: " + key + " " + fields.get(key));
	        }
	        s.append("\r\n\r\n");
	        
	        Iterator<Entry<String, String>> temps1 = maps.iterator();
	        while (temps1.hasNext())
	        {
	            Entry<String, String> keys1 = (Entry<String, String>)temps1.next();
	            String value = keys1.getKey();
	            String Key = fields.get(value);
	            if (Key.equals("Char")) {
	            	Key = "String";
				}
	            if (Key.indexOf("|PK")>0) {
					Key = Key.substring(0,Key.indexOf("|PK"));
					s.append("    @Id");
		            s.append("\r\n");
		            s.append("    @GeneratedValue(strategy = GenerationType.AUTO)");
		            s.append("\r\n");
		            s.append("    @Column(name = \"").append(value).append("\", unique = true, nullable = false)");
		            s.append("\r\n");
		            s.append("    public ").append(Key).append(" get").append(value.substring(0,1).toUpperCase()).append(value.substring(1)).append("(){");
		            s.append("\r\n");
		            s.append("        return ").append(value).append(fh);
		            s.append("\r\n");
		            s.append("    }");
		            s.append("\r\n");
		            s.append("    public void set").append(value.substring(0,1).toUpperCase()).append(value.substring(1)).append("(").append(Key).append(" ").append(value).append("){");
		            s.append("\r\n");
		            s.append("        this.").append(value).append(" = ").append(value).append(fh);
		            s.append("\r\n");
		            s.append("    }");
		            s.append("\r\n");
	            }else{
		            s.append("    @Column(name = \"").append(value).append("\")");
		            s.append("\r\n");
		            s.append("    public ").append(Key).append(" get").append(value.substring(0,1).toUpperCase()).append(value.substring(1)).append("(){");
		            s.append("\r\n");
		            s.append("        return ").append(value).append(fh);
		            s.append("\r\n");
		            s.append("    }");
		            s.append("\r\n");
		            s.append("    public void set").append(value.substring(0,1).toUpperCase()).append(value.substring(1)).append("(").append(Key).append(" ").append(value).append("){");
		            s.append("\r\n");
		            s.append("        this.").append(value).append(" = ").append(value).append(fh);
		            s.append("\r\n");
		            s.append("    }");
		            s.append("\r\n");
	            }
	        }
		} 
		s.append("\r\n\r\n");
		s.append("}");
		return s.toString();
	}
}
